Eddie's Lessons
Beginners Programming
Welcome Back...
Well here we are again; Installment eleven of the Beginning Programmer Series. This has gone much further than I first imagined when I started the series a couple years back. Eleven installments and I feel like we are only part way done. I am thrilled that you have decided to join us again on what could only be called an epic journey to learn how to program in Liberty Basic.
I hope you are beginning to feel that you understand the language a little more each installment. Additionally, I hope you are experimenting with your knowledge. This is where the rubber meets the road in the learning process.
If you remember last time, we were beginning to develop a pong based game using vector graphics. This is the hard way to go about it, but it is an excellent learning platform. We managed to develop our pong to the point of a bouncing ball that would rebound off of any of the four walls. It also had variable velocity and featured an adjustment for angle of motion.
That program will be our starting point for this month's installment. I recommend you go back and review the last installment if this seems a bit vague. This time out we will be adding a single paddle to the pong game that is user controllable. This will require that we learn to trap user input. We will also talk about sprites and how using sprites can make this process easier to manage and much more attractive.
I am going to assume you are coming up to speed on programming. We will spend less time talking about the rudimentary aspects of the programming process and concentrate on expanding our knowledge of Liberty Basic. This means that you have a good handle on the logical processes needed to break bigger problems into smaller units (and often those into still smaller units) and then develop the required programming LOGIC to solve the problem for the small unit.
This process is at the heart of programming and is a required skill to move on. If you are struggling with this, please go back and read (and reread) installment two of this series which spends considerable time on the process of programming.
Getting Down to work -
We will be adding to the program we wrote last time we were together. As I mentioned before, it is a bouncing ball simulation. To develop this into a Pong style game, we will need to add a paddle and user controls of some kind. First, lets just look at the original program:
'FullBallAnimation - By Brad Moore
'Placed into the public domain Oct 2004
xchg = -10
ychg = ((int(rnd(0)*8)-3) / (int(rnd(0)*3)+1))
xloc = 440
yloc = 80
NOMAINWIN
WindowWidth = 515 : WindowHeight = 303
UpperLeftX = INT((DisplayWidth-WindowWidth)/2)
UpperLeftY = INT((DisplayHeight-WindowHeight)/2)
graphicbox #main.gfx, 10, 10, 480, 210
button #main.go, "Go!",[go],UL, 385, 230, 105, 25
statictext #main.st1, "Speed: nn", 10, 233, 100, 25
statictext #main.st3, "Y-change: nn", 160, 233, 200, 25
Open "Window Title" for Window as #main
#main "trapclose [quit]"
#main.gfx "down; fill White; flush"
#main "font ms_sans_serif 10"
[loop]
Wait
[quit]
close #main : END
[go]
#main.gfx "color white"
timer 50 , [move]
wait
[move]
#main.st1 "Speed: " + str$(abs(xchg))
#main.st3 "Y-change: " + str$(abs(ychg))
oldx = xloc
oldy = yloc
xloc = xloc + xchg
yloc = yloc + ychg
'there is a weird bug that occasionally occurs where y can keep getting smaller
'or it can keep getting bigger - fix it here
if yloc < 0 then yloc = 0
if yloc > 210 then yloc = 210
#main.gfx "backcolor white"
#main.gfx "up; goto ";oldx;" ";int(oldy);"; down; circlefilled 10"
#main.gfx "backcolor red"
#main.gfx "up; goto ";xloc;" ";int(yloc);"; down; circlefilled 8"
if yloc < 2 or yloc > 208 then
ychg = ychg * -1
end if
if xloc < -2 or xloc > 482 then [changedirection]
wait
[changedirection]
'adjust the ball speed (x direction of travel)
ballspeed = int(rnd(0)*4)-1
direction = -1
if xchg < 0 then direction = 1
xchg = abs(xchg)
xchg = xchg + ballspeed
if xchg < 8 then xchg = 8
if xchg > 26 then xchg = 26
xchg = xchg * direction
'adjust the ball's defelction angle (y direction of travel)
direction = 1
if ychg < 0 then direction = -1
ychg = abs(ychg)
ychg = ychg + ((int(rnd(0)*9)-3) / (int(rnd(0)*3)+1))
if ychg < 0 then ychg = 0
if ychg > 10 then ychg = 11.25
ychg = ychg * direction
wait
While that seems like quite a bit of code - it really is not too much considering what we are trying to do.